﻿using UnityEngine;
using System.Collections;


public class LetterController : MonoBehaviour {

	private GameObject BoardController;
	private BoardController boardController;
	private GameController gameController;


	void Start () {
		BoardController = GameObject.FindWithTag ("BoardController");
		boardController = BoardController.GetComponent<BoardController>();
		gameController = GameObject.FindWithTag("GameController").GetComponent<GameController>();
	}

	void OnMouseDrag () {
		Vector2 mousePos = Input.mousePosition;
		Vector3 worldPos = Camera.main.ScreenToWorldPoint(mousePos);
		worldPos.z = transform.position.z;
		transform.position = worldPos;
		transform.localScale = new Vector2(1.0f, 1.0f);
		gameController.draggingLetter = true;
	}

	void OnMouseUpAsButton (){
		Vector2 mousePos = Input.mousePosition;
		Vector3 worldPos = Camera.main.ScreenToWorldPoint(mousePos);
		Vector3 boardPos = boardController.getBox(worldPos);
		Vector3 boardToWorld = boardController.convertBoardToWorld(boardPos);
		if (boardPos.x >= 0.0 && boardPos.x <= 14.0f && boardPos.y >= 0.0f && boardPos.y <= 14.0f){
			transform.position = boardToWorld;
			transform.localScale = new Vector2(0.6f, 0.6f);
			transform.localRotation = BoardController.GetComponent<Transform>().rotation;
		} else {
			transform.position = new Vector3(worldPos.x, worldPos.y, -5.0f);
			transform.localRotation = Quaternion.identity;
		};
		gameController.draggingLetter = false;
	}
}





